package com.winglish.as3.exam.opic 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import com.winglish.as3.record.Recorder;
	import com.winglish.as3.loader.SoundGroup;
	import com.winglish.as3.exam.opic.vo.OpicAnswerVo;
	import com.winglish.as3.scroll.WScrollBar;
	import com.winglish.as3.utils.QuizUtil;
	import com.winglish.as3.utils.StringUtil;
	import com.winglish.as3.event.TossNumEvent;
	
	/**
	 * ...
	 * @author charmjin
	 */
	public class OpicAnswer extends OpicAnswerVo 
	{
		
		private var dataObj:Object;
		private var soundGroup:SoundGroup;
		private var record:Recorder;
		private var myRecord:Recorder;
		
		private var qusWsb:WScrollBar;
		private var ansWsb:WScrollBar;
		private var soundAllStopFirst:Boolean = true;
		
		public function OpicAnswer(rec:Recorder, data:Object) 
		{
			dataObj = data;
			record = new Recorder();
			record.addEventListener("recListenEnd", recListenEnd);
			myRecord = rec;
			myRecord.addEventListener("recListenEnd", recListenEnd);
		}
		
		/**
		 * 녹음 듣기 끝나면 발생하는 이벤트.
		 * @param	e
		 */
		private function recListenEnd(e:Event):void {
			allStop();
		}
		
		/**
		 * 답변 관련 사운드 로드.
		 * @param	soundPath 사운드 파일 경로.
		 * @param	qusFile 질문 사운드 파일.
		 * @param	ansFile 모범 답변 사운드 파일.
		 */
		public function soundLoad(soundPath:String, qusFile:String, ansFile:String):void {
			soundGroup = new SoundGroup(new Array(qusFile, ansFile), soundPath);
			soundGroup.addEventListener("soundAllLoad", soundAllLoad);
			soundGroup.addEventListener(TossNumEvent.TOSSNUM, soundPlayEnd);
		}
		
		/**
		 * 답변 관련 사운드 로드 완료 이벤트.
		 * @param	e
		 */
		private function soundAllLoad(e:Event):void {
			dispatchEvent(new Event("ansSoundAllLoad"));
			trace("sound All Load");
			e.target.removeEventListener("soundAllLoad", soundAllLoad);
		}
		
		/**
		 * 사운드 play end 이벤트.
		 * @param	e
		 */
		private function soundPlayEnd(e:TossNumEvent):void {
			allStop();
		}
		
		/**
		 * 질문, 나의 답변, 답변 재녹음, 모범 답변 관련 버튼 셋팅.
		 * @param	path
		 */
		public function setBtn(path:MovieClip):void {
			mapping(path);
			//----------------------------------------------------------------------------------- 질문
			if (qusPlayMc) {
				qusPlayMc.addEventListener(MouseEvent.CLICK, qusPlayMcClick);
				qusPlayMc.buttonMode = true;
			}
			if (qusStopBtn) qusStopBtn.addEventListener(MouseEvent.CLICK, stopBtnClick);
			//----------------------------------------------------------------------------------- 나의 답변
			if (myPlayMc) {
				myPlayMc.addEventListener(MouseEvent.CLICK, myPlayMcClick);
				myPlayMc.buttonMode = true;
			}
			if (myStopBtn) myStopBtn.addEventListener(MouseEvent.CLICK, stopBtnClick);
			//----------------------------------------------------------------------------------- 답변 재녹음
			if (reRecMc) {
				reRecMc.addEventListener(MouseEvent.CLICK, reRecMcClick);
				reRecMc.buttonMode = true;
			}
			if (reRecStopBtn) reRecStopBtn.addEventListener(MouseEvent.CLICK, stopBtnClick);
			if (rePlayMc) {
				rePlayMc.addEventListener(MouseEvent.CLICK, rePlayMcClick);
				rePlayMc.buttonMode = true;
			}
			if (reStopBtn) reStopBtn.addEventListener(MouseEvent.CLICK, stopBtnClick);
			//----------------------------------------------------------------------------------- 모범 답변
			if (ansPlayMc) {
				ansPlayMc.addEventListener(MouseEvent.CLICK, ansPlayMcClick);
				ansPlayMc.buttonMode = true;
			}
			if (ansStopBtn) ansStopBtn.addEventListener(MouseEvent.CLICK, stopBtnClick);
			
		}
		
		/**
		 * 질문 play 버튼 클릭 이벤트.
		 * @param	e
		 */
		private function qusPlayMcClick(e:MouseEvent):void 
		{
			allStop();
			soundGroup.soundPlay(0);
			e.target.gotoAndStop(2);
		}
		
		/**
		 * 나의 답변 play 버튼 클릭 이벤트.
		 * @param	e
		 */
		private function myPlayMcClick(e:MouseEvent):void 
		{
			allStop();
			myRecord.listen();
			e.target.gotoAndStop(2);
		}
		
		/**
		 * 답변 재녹음 rec 버튼 클릭 이벤트.
		 * @param	e
		 */
		private function reRecMcClick(e:MouseEvent):void 
		{
			allStop();
			record.start();
			e.target.gotoAndStop(2);
		}

		/**
		 * 답변 재녹음 play 버튼 이벤트.
		 * @param	e
		 */
		private function rePlayMcClick(e:MouseEvent):void 
		{
			allStop();
			record.listen();
			e.target.gotoAndStop(2);
		}
		
		/**
		 * 모범 답변 play 버튼 이벤트.
		 * @param	e
		 */
		private function ansPlayMcClick(e:MouseEvent):void 
		{
			allStop();
			soundGroup.soundPlay(1);
			e.target.gotoAndStop(2);
		}
		
		/**
		 * 모든 stop 버튼 클릭 이벤트.
		 * @param	e
		 */
		private function stopBtnClick(e:MouseEvent):void 
		{
			allStop();
		}
		
		/**
		 * 녹음, 사운드 정지.
		 */
		public function allStop():void {
			record.stop();
			record.stopListen();
			myRecord.stop();
			myRecord.stopListen();
			soundGroup.soundAllStop();
			
			if (qusPlayMc) qusPlayMc.gotoAndStop(1);
			if (myPlayMc) myPlayMc.gotoAndStop(1);
			if (reRecMc) reRecMc.gotoAndStop(1);
			if (rePlayMc) rePlayMc.gotoAndStop(1);
			if (ansPlayMc) ansPlayMc.gotoAndStop(1);
			if (soundAllStopFirst) {
				soundAllStopFirst = false;
				dispatchEvent(new Event("soundAllStopFirst"));
			}
		}
		
		/**
		 * 문제 텍스트 셋팅. 
		 * @param	path
		 * @param	eng
		 * @param	kor
		 */
		public function setQustionTxt(path:MovieClip, eng:TextField, kor:TextField = null):void {
			engTxt = eng;
			engTxt.multiline = true;
			engTxt.autoSize = TextFieldAutoSize.LEFT;
			engTxt.text = QuizUtil.newLine(dataObj.queEng);
			
			if (kor) {
				korTxt = kor;
				korTxt.multiline = true;
				korTxt.autoSize = TextFieldAutoSize.LEFT;
				korTxt.text = QuizUtil.newLine(dataObj.queKor);
				korTxt.y = engTxt.y + engTxt.height + 5;
			}
				
			if (qusScrollBar) qusWsb = WScrollBar.GET( path,  { width: path.masker.width, height: path.masker.height , offset: 5 } );
		}
		
		/**
		 * 답변 텍스트 셋팅.
		 * @param	path 문제 텍스트의 무비클립.
		 * @param	eng 영문 텍스트 필드.
		 * @param	kor 한글 텍스트 필드.
		 * @param   strategy 전략 텍스트 필드.
		 * @param   voca 단어 텍스트 필드.
		 */
		public function setAnswerTxt(path:MovieClip, eng:TextField, kor:TextField, strategy:TextField = null, voca:TextField = null):void {
			engTxt = eng;
			engTxt.multiline = true;
			engTxt.autoSize = TextFieldAutoSize.LEFT;
			engTxt.text = QuizUtil.newLine(dataObj.ansEng);
			
			korTxt = kor;
			korTxt.multiline = true;
			korTxt.autoSize = TextFieldAutoSize.LEFT;
			korTxt.text = QuizUtil.newLine(dataObj.ansKor);
			korTxt.parent.y = engTxt.parent.y + engTxt.parent.height + 5;
			
			if (strategy) {
				
			}
			
			if (voca) {
				
			}
			
			if (ansScrollBar) ansWsb = WScrollBar.GET( path,  { width: path.masker.width, height: path.masker.height , offset: 5 } );
		}
	}
}